package cn.est.controller;
import cn.est.pojo.Appointment;
import cn.est.constants.Constants;
import cn.est.dto.Page;
import cn.est.dto.Result;
import cn.est.service.AppointmentService;
import cn.est.utils.EmptyUtils;
import cn.est.utils.ResultUtils;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;

@Controller
@RequestMapping("/client/appointment")
public class AppointmentController {

    @Resource
    private AppointmentService appointmentService;

    @RequestMapping("/search")
    @ResponseBody
    public Page<Appointment> searchAppointment(@RequestParam Map<String,Object> param) {
        Page<Appointment> appointmentPage = null;
        try {
            Integer pageNo= EmptyUtils.isEmpty(param.get("pageNo"))? Page.DEFAULT_PAGE_NO:Integer.parseInt((String) param.get("pageNo"));
            Integer pageSize= EmptyUtils.isEmpty(param.get("pageSize"))? Page.DEFAULT_PAGE_SIZE:Integer.parseInt((String) param.get("pageSize"));
            appointmentPage  = appointmentService.queryAppointmentPageByMap(param, pageNo, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return appointmentPage;
    }

    @RequestMapping("/saveOrUpdateAppointment")
    @ResponseBody
    public Result saveOrUpdateAppointment(Appointment appointment){
        Integer flag=0;
        try {
            if(EmptyUtils.isEmpty(appointment.getId())){
                flag=appointmentService.qdtxAddAppointment(appointment);
            }else{
                flag=appointmentService.qdtxModifyAppointment(appointment);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag>=1?ResultUtils.returnSuccess():ResultUtils.returnFail();
    }

    @RequestMapping("/getAppointmentById")
    @ResponseBody
    public Result getAppointmentById(Long id){
        Result result= ResultUtils.returnSuccess();
        Appointment appointment=null;
        try {
            appointment=appointmentService.getAppointmentById(id);
            result.setData(appointment);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/batchRemoveAppointment")
    @ResponseBody
    public Result batchRemoveAppointment(String ids){
        Integer flag=0;
        try {
            flag=appointmentService.qdtxBatchDeleteAppointment(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtils.returnSuccess();
    }

    @RequestMapping("/removeAppointmentById")
    @ResponseBody
    public Result removeAppointmentById(Long id){
        Integer flag=0;
        try {
            flag=appointmentService.qdtxDeleteAppointmentById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtils.returnSuccess();
    }
}
